package ee.homework.fourth;

import java.util.Random;

public class HashFunction {

	public static void main(String[] args) {
		HashFunction hf = new HashFunction();
		int[] slotCount = new int[32];
		// for (int i = 1; i <= 1000; i++) {
		int key = 5;
		int slot = hf.multiplicationMethod(17011, 16, 5, key);
		System.out.println("slot: " + slot + ", key: " + key);
		// slotCount[slot]++;
		// }
		// for (int i = 0; i < slotCount.length; i++) {
		// System.out.println("Slot " + (i + 1) + " has " + slotCount[i]
		// + " elements");
		// }
		hf.findKeys(17011, 16, 5, 3);
	}

	public int multiplicationMethod(int A, int w, int r, int k) {
		int remainder = (A * k) % (int) Math.pow(2, w);
		int slot = (remainder >> (w - r));
		return slot;
	}

	public void findKeys(int A, int w, int r, int slot) {
		int remainder = (slot << (w - r));
		System.out.println("remainder: " + remainder);
		int modDiv = (int) Math.pow(2, w);
		Random rand = new Random();
		int rem = 1;
		int mod = remainder + modDiv - 1;
		System.out.println("mod1: " + mod);
		while (rem != 0) {
			mod++;
			System.out.println("mod: " + mod);
			rem = mod % A;
			if (mod == remainder + modDiv + 2047) {
				System.out.println("EXIT");
				break;
			}
		}
		System.out.println("key: " + mod / A);
	}

	public void ff(int A, int w, int r, int k) {

	}
}
